package main;

import java.io.File;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import javax.xml.bind.*;
import org.hibernate.Session;
import org.hibernate.Transaction;
import main.hw1model.*;
import main.hw1model.Airline;
import main.hw1model.Airport;
import main.hw2model.*;
import main.util.HibernateContext;

public class JAXBUnMarshaller {
	public static void main(String[] args) {
		
		
		Scanner in = new Scanner(System.in);
		boolean hasInput = true;
		while (hasInput)
		{
			System.out.println("Select table which needs to be created in the Mysql database"); 
			System.out.println("1. Airport ");
			System.out.println("2. Airline ");
			System.out.println("3. Airport Company ");
			System.out.println("4. Airport Description");
			System.out.println("5. Quit");
			System.out.println("Type a number from 1 - 5");
			int input = in.nextInt();
			switch (input)
			{
				case 1:
					File file1 = new File("airport.xml");
					(new JAXBUnMarshaller()).unMarshall(file1, "airport");
					break;
				case 2:
					File file2 = new File("airline.xml");
					(new JAXBUnMarshaller()).unMarshall(file2, "airline");
					break;
				case 3:
					File file3 = new File("airportcompany.xml");
					(new JAXBUnMarshaller()).unMarshall(file3, "airport company");
					break;
				case 4:
					File file4 = new File("airport.xml");
					(new JAXBUnMarshaller()).unMarshall(file4, "airport description");
					break;
				case 5:
					hasInput = false; 
					break;
				default:
					hasInput = false; 
			}
		}
	}
	
	/**
	 * Unmarshall XML files 
	 */
	public void unMarshall(File file, String type) {
		
		try {
			JAXBContext jaxbContext = JAXBContext.newInstance("main.hw2model");
			Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();
			if(type.equals("airport"))
			{
				JAXBElement<AirportList> airportListElement = 
					(JAXBElement<AirportList>) unMarshaller.unmarshal(file);
				AirportList portList = airportListElement.getValue();
				System.out.println(portList.getAirport());
				List<main.hw2model.Airport> airportList = portList.getAirport();
				for(main.hw2model.Airport port : airportList) {
					String name = port.getName();
					System.out.println("Import Airport Name: " + name);
					Session session = HibernateContext.getSession();
					Transaction tx = session.beginTransaction();
					
					session.save(new main.hw1model.Airport(name));
					
					tx.commit();
					session.close();
					
				}
			}
			else if ( type.equals("airline"))
			{
				JAXBElement<AirlineList> airlineListElement = 
					(JAXBElement<AirlineList>) unMarshaller.unmarshal(file);
				AirlineList list = airlineListElement.getValue();
				System.out.println(list.getAirline());
				List<main.hw2model.Airline> airlineList = list.getAirline();
				for(main.hw2model.Airline airline: airlineList)
				{
					int flightNum = (int)airline.getFlightNumber();
					int originId = (int)airline.getORIGINAIRPORTID();
					String origin = airline.getORIGIN();
					String destination = airline.getDESTINATION();
					Session session = HibernateContext.getSession();
					
			        Transaction tx = session.beginTransaction();
			        {
						session.save(new main.hw1model.Airline
								(flightNum,originId,origin,destination));
					}
			        tx.commit();
			        session.close();
				}
			}	
			else if (type.equals("airport company"))
			{
				JAXBElement<AirportcompanyList> companyListElement =
					(JAXBElement<AirportcompanyList>) 
					unMarshaller.unmarshal(file);
				AirportcompanyList companyList = companyListElement.getValue();
				System.out.println(companyList.getAirportcompany());
				List<main.hw2model.Airportcompany> airportCompanyList =
						companyList.getAirportcompany();
				for(main.hw2model.Airportcompany company: airportCompanyList)
				{
					Session session = HibernateContext.getSession();
					
			        Transaction tx = session.beginTransaction();
			        {
			        	String abr = company.getAbr();
						session.save(new main.hw1model.AirportCompany(abr));
					}
			        tx.commit();
			        session.close();
				}
			}
			else if (type.equals("airport description"))
			{  
				JAXBElement<AirportList> airportListElement = 
						(JAXBElement<AirportList>) unMarshaller.unmarshal(file);
					AirportList portList = airportListElement.getValue();
					System.out.println(portList.getAirport());
					List<main.hw2model.Airport> airportList = portList.getAirport();
					for(main.hw2model.Airport port : airportList) {
						
						main.hw2model.Airportdesc description = port.getAirportdesc();
						String origin = description.getORIGIN();
						String originCityName = description.getORIGINCITYNAME();
						String originStateAbr = description.getORIGINSTATEABR();
						System.out.println("Import Airport Description: " + origin + "|" 
								+ originCityName + "|" + originStateAbr);
						Session session = HibernateContext.getSession();
						Transaction tx = session.beginTransaction();
						
						session.save(new main.hw1model.AirportDesc(origin, originCityName, originStateAbr));
						
						tx.commit();
						session.close();	
					}
			}
			else
			{}
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
